package com.nettest.util;

import java.util.Map;

import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletContextAware;
@Service
public class AppUtil implements ApplicationContextAware,ServletContextAware {
	private static Log logger = LogFactory.getLog(AppUtil.class);
	private static ServletContext servletContext = null;
	private static ApplicationContext appContext;
	private static Map<String, Document> orgMenus = null;
	private static Map<String, Document> itemsMenus = null;
	private static Document menuDocument = null;

	public void setApplicationContext(ApplicationContext paramApplicationContext)
			throws BeansException {
		appContext = paramApplicationContext;
	}

	public static Map<String, Document> getOrgMenus() {
		return orgMenus;
	}

	public static Map<String, Document> getItemsMenus() {
		return itemsMenus;
	}

	public static Document getMenuDocument() {
		return menuDocument;
	}

	public static Object getBean(String paramString) {
		return appContext.getBean(paramString);
	}

	public static String getAppAbsolutePath() {
		return servletContext.getRealPath("/");
	}

	public static String getMenuAbDir() {
		return getAppAbsolutePath() + "/js/menu/xml/";
	}

	public static String getMenuXslDir() {
		return getAppAbsolutePath() + "/js/menu/";
	}
	@PostConstruct
	public synchronized void init() {
		reloadMenu();
	}

	public static void reloadMenu() {
		orgMenus = MenuUtil.getAllOrgMenus();
		itemsMenus = MenuUtil.getAllItemsMenus(orgMenus);
		menuDocument = MenuUtil.mergeOneDoc(orgMenus);
	}

	public static void synMenu() {
		/*AppFunctionService localAppFunctionService = (AppFunctionService) getBean("appFunctionService");
		FunUrlService localFunUrlService = (FunUrlService) getBean("funUrlService");
		Iterator localIterator = orgMenus.keySet().iterator();
		while (localIterator.hasNext()) {
			Document localDocument = (Document) orgMenus.get(localIterator
					.next());
			List localList1 = localDocument.getRootElement().selectNodes(
					"/Menus/Items//Item/Function");
			for (int i = 0; i < localList1.size(); ++i) {
				Element localElement = (Element) localList1.get(i);
				String str1 = localElement.attributeValue("id");
				String str2 = localElement.attributeValue("text");
				AppFunction localAppFunction = localAppFunctionService
						.getByKey(str1);
				if (localAppFunction == null)
					localAppFunction = new AppFunction(str1, str2);
				else
					localAppFunction.setFunName(str2);
				List localList2 = localElement.selectNodes("./url");
				localAppFunctionService.save(localAppFunction);
				for (int j = 0; j < localList2.size(); ++j) {
					Node localNode = (Node) localList2.get(j);
					String str3 = localNode.getText();
					FunUrl localFunUrl = localFunUrlService.getByPathFunId(
							str3, localAppFunction.getFunctionId());
					if (localFunUrl != null)
						continue;
					localFunUrl = new FunUrl();
					localFunUrl.setUrlPath(str3);
					localFunUrl.setAppFunction(localAppFunction);
					localFunUrlService.save(localFunUrl);
				}
			}
		}*/
	}

	public static Document getGrantMenuDocument() {
		String str = servletContext.getRealPath("/js/menu") + "/menu-grant.xsl";
		Document localDocument = null;
		try {
			localDocument = XmlUtil.styleDocument(menuDocument, str);
		} catch (Exception localException) {
			logger.error("menu-grant.xsl transform has error:"
					+ localException.getMessage());
		}
		return localDocument;
	}

	
	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
}